package com.lian.hope.controller;

import com.lian.hope.annotations.RequiredPermission;
import com.lian.hope.enums.PermissionType;
import com.lian.hope.model.dto.NotificationDTO;
import com.lian.hope.model.vo.NotificationVO;
import com.lian.hope.service.NotificationService;
import com.lian.hope.utils.JwtUtil;
import com.lian.hope.utils.Result;
import com.lian.hope.websocket.WebSocketServer;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notifications")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "通知模块相关接口", description = "通知模块相关接口")
public class NotificationController {

    private final NotificationService notificationService;

    private final WebSocketServer webSocketServer;

    @GetMapping("/{notificationTypeId}")
    @Operation(summary = "根据用户id以及通知类型获取通知列表")
    public Result<List<NotificationVO>> getNotification(@RequestHeader String Authorization,
        @Parameter(description = "通知类型") @PathVariable Integer notificationTypeId) {
        Map<String, Object> claims = JwtUtil.parseToken(Authorization);
        Long userId = Long.parseLong(claims.get("userId").toString());
        List<NotificationVO> notificationVOS = notificationService.getNotification(userId, notificationTypeId);
        return Result.success(notificationVOS);
    }

    @GetMapping("/system")
    @Operation(summary = "获取系统通知")
    public Result<List<NotificationVO>> getSystemNotification(@RequestHeader String Authorization) {
        Map<String, Object> claims = JwtUtil.parseToken(Authorization);
        Long userId = Long.parseLong(claims.get("userId").toString());
        log.info("系统通知");
        return Result.success(notificationService.getSystemNotification(userId));
    }

    @Operation(summary = "获取活动通知")
    @GetMapping("/activity")
    public Result<List<NotificationVO>> getActivityNotification(@RequestHeader String Authorization) {
        Map<String, Object> claims = JwtUtil.parseToken(Authorization);
        Long userId = Long.parseLong(claims.get("userId").toString());
log.info("活动通知");


        return  Result.success(notificationService.getActivityNotification(userId));
    }



    /**
     * 创建通知
     *
     * @param notificationDTO
     * @return
     */

    @Operation(summary = "新增通知")
    @PostMapping
    @RequiredPermission(PermissionType.NOTIFICATION)
    public Result insertNotification(@RequestBody NotificationDTO notificationDTO) {
        String message = notificationDTO.getNotificationType() == 0 ? "收到来自系统的通知" : "收到来自活动的通知";

        // 判断接受者id是否为空
        if (notificationDTO.getReceiverId() == null) {
            // 如果为空则发送给所有用户
            webSocketServer.sendToAllClient(message);
        } else {
            // 如果不为空则发送给指定用户
            webSocketServer.sendToOneClient(message, notificationDTO.getReceiverId().toString());
        }
        notificationService.insertNotification(notificationDTO);
        return Result.success();
    }

    //    TODO: 后续实现删除通知功能
}
